const path = require("path");
const { uploadError } = require("../constant/err.type");
class UploadController {
  async upload(ctx, next) {
    // 获取上传的文件
    const { file } = ctx.request.files;
    // 如果上传的文件存在
    if (file) {
      // 返回上传成功的信息
      ctx.response.body = {
        code: "10008",
        message: "图片上传成功",

        data: {
          url: path.basename(file.filepath),
        },
      };
    } else {
      return ctx.app.emit("error", uploadError, ctx);
    }
    await next();
  }
}

module.exports = new UploadController();
